﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAO
{
     public class CameraDAO
    {
         CameraDBEntities db = new CameraDBEntities();
         public List<Camera> GetListAll()
         {
             return db.Cameras.Where(x=>x.camStatus== true).ToList();
         }
         public List<Camera> GetListAll_Sort()
         {
             return db.Cameras.Where(x => x.camStatus == true).OrderByDescending(x=>x.camId).ToList();
         }
         public List<Camera> GetListTop10()
         {
             return db.Cameras.Where(x => x.camStatus == true).Take(10).ToList();
         }
         public Camera GetById(int id)
         {
             return db.Cameras.Where(x => x.camId == id && x.camStatus == true).SingleOrDefault();
         }

         public List<Camera> GetListSearch(string tukhoa)
         {
             List<Camera> listkq = new List<Camera>();
             listkq = db.Cameras.Where(x => x.camStatus == true).ToList();
             listkq = listkq.FindAll(

                 delegate(Camera math)
                 {
                     if (Utils.ConvertToUnSign(math.camNames.ToLower()).Contains(tukhoa))
                         return true;
                     else
                         return false;
                 }
             );
             return listkq;
         }
         public bool Insert(Camera c)
         {
             try
             {
                 db.Cameras.Add(c);
                 db.SaveChanges();
                 return true;
             }
             catch
             {
                 return false;
             }
         }

         public bool Edit(Camera ca)
         {
             try
             {
                 Camera a = new Camera();
                 a = db.Cameras.Where(x=>x.camId== ca.camId).SingleOrDefault();
                 a.camNames = ca.camNames;
                 a.camImages = ca.camImages;
                 a.camPrice = ca.camPrice;
                 a.camDetails = ca.camDetails;
                 a.camMaxResolution = ca.camMaxResolution;
                 a.camSensorType = ca.camSensorType;
                 a.camSensorSize = ca.camSensorSize;
                 a.camIso = ca.camIso;
                 a.camFocalLengthMulti = ca.camFocalLengthMulti;
                 a.camScreenSize = ca.camScreenSize;
                 a.camMinShutterSpeed = ca.camMinShutterSpeed;
                 a.camMaxShutterSpeed = ca.camMaxShutterSpeed;
                 a.modelId = ca.modelId;
                 a.manufacturerId = ca.manufacturerId;
                 a.seriesId = ca.seriesId;
                 a.camStatus = ca.camStatus;
                 db.SaveChanges();
                 return true;
             }
             catch
             {
                 return false;
             }
         }

         public bool Delete(int id)
         {
             try
             {
                 Camera a = new Camera();
                 a = db.Cameras.Where(x => x.camId == id).SingleOrDefault();
                 db.Cameras.Remove(a);

                  
                 db.SaveChanges();
                 return true;
             }
             catch
             {
                 return false;
             }
         }

    }
}
